﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using zyconfig;

namespace zyui
{
    interface IStringAna
    {
        /// <summary>
        /// key:文本包含的汉字或英文单词，value:汉字或英文单词出现的次数
        /// </summary>
        /// <param name="text"></param>
        /// <returns>key:文本包含的汉字或英文单词，value:汉字或英文单词出现的次数</returns>
        Dictionary<string, int> Ana(string text);
    }
    public class StringAna : IStringAna
    {
        public static string getPin1(string item,string nt)
        {
            string str = string.Empty;
            if (Regex.IsMatch(item.ToString(), @"[\u4e00-\u9fbb]"))
            {
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = " 汉字='" + item.ToString() + "'";
                    object[] _value = _open.GetFields("拼音库", "简单拼音,复杂拼音");
                    string py = _value[1].ToString().Trim();
                    string[] arry = py.Split(new char[] { ' ' });
                    string s = "";
                    for (int i = 0; i < arry.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(arry[i]))
                            s += arry[i] + ";";
                    }
                    if (!string.IsNullOrEmpty(s))
                    {
                        s = s.Substring(0, s.Length - 1);
                        string[] arrys = s.Split(';');
                        if (arrys.Length == 1)
                        {
                            if (arry[0].Length > 6)
                            {
                                str = arry[0].Substring(0, 6).Trim() + " ";
                            }
                            else
                                str = arry[0].Trim() + " ";
                        }
                        else
                        {
                            FrmPyxz iso = new FrmPyxz();
                            iso.arry = arrys;
                            iso.hz = item;
                            iso.nt = nt;
                            if (iso.ShowDialog() == DialogResult.OK)
                            {
                                str = iso.str + " ";
                            }
                        }
                    }
                }
            }
            else
                str = item + " ";
            //Console.WriteLine(str);
            return str;
        }
        public static string getPin(string item)
        {
            string str=string.Empty;
            if (Regex.IsMatch(item.ToString(), @"[\u4e00-\u9fbb]"))
            {
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = " 汉字='" + item.ToString() + "'";
                    object[] _value = _open.GetFields("拼音库", "简单拼音,复杂拼音");
                    string py = _value[1].ToString().Trim();
                    string[] arry = py.Split(new char[]{ ' '});
                    string s = "";
                    for (int i = 0; i < arry.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(arry[i]))
                            s += arry[i] + ";";
                    }
                    if (!string.IsNullOrEmpty(s))
                    {
                        s = s.Substring(0, s.Length - 1);
                        string[] arrys = s.Split(';');
                        if (arrys.Length == 1)
                        {
                            if (arry[0].Length > 6)
                            {
                                str = arry[0].Substring(0, 6).Trim() + " ";
                            }
                            else
                                str = arry[0].Trim() + " ";
                        }
                        else
                        {
                            FrmPyxz iso = new FrmPyxz();
                            iso.arry = arrys;
                            iso.hz = item;
                            if (iso.ShowDialog() == DialogResult.OK)
                            {
                                str = iso.str+" ";
                            }
                        }
                    }
                }
            }
            else
                str = item + " ";
            //Console.WriteLine(str);
            return str;
        }
        public static List<string> getString(string text)
        {
            var words = new List<string>();
            byte[] bts = Encoding.Unicode.GetBytes(text);
            var pointer = 0;
            var i = 0;
            while (i <= bts.Length - 2)
            {
                byte[] tmp;

                if (bts[i + 1] == 0 && bts[i] != 32)
                {
                    pointer = i;
                    while (pointer + 2 < bts.Length && bts[pointer + 2] != 32 && bts[pointer + 2 + 1] == 0)
                    {
                        pointer += 2;
                    }
                    var len = pointer + 2 - i;
                    tmp = new byte[len];
                    Array.Copy(bts, i, tmp, 0, len);
                    i = pointer + 2;
                }
                else if (bts[i] == 32 && bts[i + 1] == 0)
                {
                    i += 2;
                    continue;
                }
                else
                {
                    tmp = new byte[] { bts[i], bts[i + 1] };
                    i += 2;
                }
                var word = Bytes2Word(tmp);
                words.Add(word);
            }
            return words;
        }
        public Dictionary<string, int> Ana(string text)
        {
            var words = new List<string>();
            var dict = new Dictionary<string, int>();
            byte[] bts = Encoding.Unicode.GetBytes(text);
            //foreach (var bt in bts)
            //{
            //    Console.Write(string.Format("{0} ",bt));
            //}
            //Console.WriteLine();
            var pointer = 0;
            var i = 0;
            while (i <= bts.Length - 2)
            {
                byte[] tmp;

                if (bts[i + 1] == 0 && bts[i] != 32)
                {
                    pointer = i;
                    while (pointer + 2 < bts.Length && bts[pointer + 2] != 32 && bts[pointer + 2 + 1] == 0)
                    {
                        pointer += 2;
                    }
                    var len = pointer + 2 - i;
                    tmp = new byte[len];
                    Array.Copy(bts, i, tmp, 0, len);
                    i = pointer + 2;
                }
                else if (bts[i] == 32 && bts[i + 1] == 0)
                {
                    i += 2;
                    continue;
                }
                else
                {
                    tmp = new byte[] { bts[i], bts[i + 1] };
                    i += 2;
                }
                var word = Bytes2Word(tmp);
                words.Add(word);
                Put(dict, Bytes2Word(tmp));
            }
            return dict;
        }
        static string Bytes2Word(byte[] bytes)
        {
            return Encoding.Unicode.GetString(bytes);
        }

        void Put(Dictionary<string, int> dict, string word)
        {
            int value;
            if (dict.TryGetValue(word, out value))
            {
                dict[word] = value + 1;
            }
            else
            {
                dict[word] = 1;
            }
        }
    }
}
